package com.hp;

import com.hp.pojo.MyBatisUtils;
import com.hp.pojo.Orders;
import com.hp.pojo.Person;
import com.hp.pojo.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;

public class MyBatisTest {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void init() {
        //定义读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;
        try {
            //读取mybatis-config.xml文件到reader对象中
            reader = Resources.getResourceAsReader(resources);
            //初始化mybatis,创建SqlSessionFactory类的对象
            SqlSessionFactory sqlMapper = new
                    SqlSessionFactoryBuilder().build(reader);
            //创建session对象
            sqlSession = sqlMapper.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void findPersonByIdTest(){
        SqlSession session= MyBatisUtils.getSession();
        Person person=session.selectOne("com.hp.mapper."+"PersonMapper.findPersonById",1);
        System.out.println(person);
        session.close();
    }
    @Test
    public void findUserTest() {

        SqlSession session = MyBatisUtils.getSession();

        Users users = session.selectOne("com.hp.mapper."
                + "UsersMapper.findUserWithOrders", 1);

        System.out.println(users);

        session.close();
    }
    @Test
    public void findOrdersTest() {

        SqlSession session = MyBatisUtils.getSession();

        Orders orders = session.selectOne("com.hp.mapper."
                + "OrdersMapper.findOrdersWithPorduct", 1);

        System.out.println(orders);

        session.close();
    }
}
